//
//  SettingView.m
//  YTAutoLayout
//
//  Created by fly on 14/12/12.
//  Copyright (c) 2014年 fly. All rights reserved.
//

#import "AdSettingView.h"
#import "Constant.h"
#import "EnvironmentVariable.h"

@interface AdSettingView()<UIScrollViewDelegate>

@end
@implementation AdSettingView

-(instancetype)init
{
    if(self= [super init])
    {
        [self createAdSettingView];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoradWasChanged:) name:UIKeyboardDidShowNotification object:nil];
    }
    return self;
}

/**
 *  获取视图
 */
-(void)createAdSettingView
{
    
    [self addSubview:[[UIView alloc]init]];
    self.backgroundColor = [UIColor whiteColor];
    UIFont *font = [UIFont systemFontOfSize:13];
    //1、初始化一个scrollview
    UIScrollView *scrollView  = [[UIScrollView alloc]init];
    
    _scrollView = scrollView;
    scrollView.delegate  =self;
    
    scrollView.translatesAutoresizingMaskIntoConstraints = YES;
    
    scrollView.frame = [[UIScreen mainScreen] bounds];
    
    scrollView.contentSize = [[UIScreen mainScreen] bounds].size;
    
    UIView *cView  = [[UIView alloc] init];
    
    CGRect tempr = [[UIScreen mainScreen] bounds];
    tempr.origin.y += 74;
    tempr.size.height  = tempr.size.height = 400;
    cView.frame = tempr;
    
    //设置cview的约束
    //cView.autoresizingMask = ;
    [scrollView addSubview:cView];
    
    
    UILabel *labAddress  = [[UILabel alloc] init];
    labAddress.textColor = [UIColor grayColor];
    labAddress.font =font;
    labAddress.text  = @"服务地址";
    
    UILabel *labProt  = [[UILabel alloc] init];
    labProt.textColor = [UIColor grayColor];
    labProt.font =font;
    labProt.text  = @"服务端口";
    
    UILabel *labPath  = [[UILabel alloc] init];
    labPath.textColor = [UIColor grayColor];
    labPath.font =font;
    labPath.text  = @"服务路径";
    
    UILabel *labName  = [[UILabel alloc] init];
    labName.textColor = [UIColor grayColor];
    labName.font =font;
    labName.text  = @"应用名称";
    
    UILabel *labData  = [[UILabel alloc] init];
    labData.textColor = [UIColor grayColor];
    labData.font =font;
    labData.text  = @"数据服务";
    
    UILabel *labMark  = [[UILabel alloc] init];
    labMark.textColor = [UIColor grayColor];
    labMark.font =font;
    labMark.text  = @"数据标示";
    
    
    UILabel *labAD  = [[UILabel alloc] init];
    labAD.textColor = [UIColor grayColor];
    labAD.font =font;
    labAD.text  = @"AD域用户";
    
    
    UITextField *textAddress  = [[UITextField alloc] init];
    textAddress.borderStyle = UITextBorderStyleRoundedRect;
    _textAddress = textAddress;
    
    UITextField *textProt  = [[UITextField alloc] init];
    textProt.borderStyle = UITextBorderStyleRoundedRect;
    self.textProt = textProt;
    
    UITextField *textPath  = [[UITextField alloc] init];
    textPath.borderStyle = UITextBorderStyleRoundedRect;
    self.textPath =textPath;
    
    UITextField *textName  = [[UITextField alloc] init];
    textName.borderStyle = UITextBorderStyleRoundedRect;
    self.textName = textName;
    
    UITextField *textData  = [[UITextField alloc] init];
    textData.borderStyle = UITextBorderStyleRoundedRect;
    self.textData = textData;
    
    UITextField *textMark  = [[UITextField alloc] init];
    textMark.borderStyle = UITextBorderStyleRoundedRect;
    self.textMark = textMark;
    
    UISwitch *swtAD = [[UISwitch alloc] init];
    _isAd = swtAD;
    
    UIButton *btnSave = [[UIButton alloc] init];
    [btnSave setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnSave setBackgroundColor:EF_OLD_RED_COLOR];
    [btnSave setTitle:@"保存" forState:UIControlStateNormal];
    [btnSave addTarget:self action:@selector(btnSave) forControlEvents:UIControlEventTouchDown];
    
    //添加到子控件到View
    
    [cView addSubview:labAD];
    [cView addSubview:labAddress];
    [cView addSubview:labData];
    [cView addSubview:labMark];
    [cView addSubview:labName];
    [cView addSubview:labPath];
    [cView addSubview:labProt];
    
    [cView addSubview:textAddress];
    [cView addSubview:textData];
    [cView addSubview:textMark];
    [cView addSubview:textName];
    [cView addSubview:textPath];
    [cView addSubview:textProt];
    
    [cView addSubview:btnSave];
    [cView addSubview:swtAD];
    [self addSubview:scrollView];
    //cView.translatesAutoresizingMaskIntoConstraints = NO;

    for (UIView *item in cView.subviews) {
        item.translatesAutoresizingMaskIntoConstraints = NO;
    }
    
    #pragma mark --进行约束

    //====================================================
    NSMutableArray *addressConstraints = [[NSMutableArray alloc] init];
    //
    [addressConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[labAddress(==60)][textAddress]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(labAddress,textAddress)]];
    
    [addressConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[labProt(==60)][textProt]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(labProt,textProt)]];
    
    [addressConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[labPath(==60)][textPath]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(labPath,textPath)]];
    
    [addressConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[labName(==60)][textName]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(labName,textName)]];
    
    [addressConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[labData(==60)][textData]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(labData,textData)]];
    
    [addressConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[labMark(==60)][textMark]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(labMark,textMark)]];
    
    [addressConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[labAD(==60)][swtAD]-10-|" options:NSLayoutFormatAlignAllBottom metrics:nil views:NSDictionaryOfVariableBindings(labAD,swtAD)]];
    
    
    //设置垂直约束
    [addressConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[labAddress(==40)]-10-[labProt(==40)]-10-[labPath(==40)]-10-[labName(==40)]-10-[labData(==40)]-10-[labMark(==40)]-10-[labAD(==40)]" options:NSLayoutFormatAlignAllLeft metrics:nil views:NSDictionaryOfVariableBindings(labAddress,labProt,labPath,labName,labData,labMark,labAD)]];
    
    [addressConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[textAddress(==40)]-10-[textProt(==40)]-10-[textPath(==40)]-10-[textName(==40)]-10-[textData(==40)]-10-[textMark(==40)]-10-[swtAD]" options:NSLayoutFormatAlignAllLeft metrics:nil views:NSDictionaryOfVariableBindings(textAddress,textProt,textPath,textName,textData,textMark,swtAD)]];
    
    [cView addConstraints:addressConstraints];
    
    addressConstraints = nil;
    
    //btnSave的约束
    //======================================================
    
    NSMutableArray *saveConstraints = [[NSMutableArray alloc] init];
    //设置cView水平约束
    [saveConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-10-[btnSave]-10-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(btnSave)]];
    //设置cView的垂直约束
    [saveConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[labAD]-[btnSave(==44)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(labAD,btnSave)]];
    [cView addConstraints:saveConstraints];
    
    saveConstraints = nil;
    
    
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark scrollView
float tempY ;
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    tempY = 0;
    tempY = scrollView.contentOffset.y;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if(tempY-scrollView.contentOffset.y >10 )
    {
        [self endEditing:YES];
    }
    
    
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    tempY = 0;
}

-(void) keyBoradWasChanged:(NSNotification*) note
{
    NSDictionary *info  =note.userInfo;
    CGRect beginUse = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGRect endUse = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    
    if(endUse.origin.y+beginUse.size.height ==beginUse.origin.y)
    {
        CGRect f =  _scrollView.frame ;
        f.size.height = f.size.height - endUse.size.height;
        //开始
        
        [UIView animateWithDuration:0.25 animations:^{
           _scrollView.contentInset = UIEdgeInsetsMake(0, 0, f.size.height-([UIScreen mainScreen].bounds.size.height-400), 0);
        }];

        
    }
    else if(endUse.origin.y==beginUse.origin.y+beginUse.size.height)
    {
        //隐藏
        CGRect f =  _scrollView.frame ;
        f.size.height = f.size.height + endUse.size.height;
        [UIView animateWithDuration:0.25 animations:^{
            _scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        }];

        
        
    }
    else {
        //第三方键盘
        CGRect f =  [[UIScreen mainScreen] bounds];
        f.size.height = f.size.height - endUse.size.height;
        [UIView animateWithDuration:0.25 animations:^{
            _scrollView.contentInset = UIEdgeInsetsMake(0, 0, f.size.height-([UIScreen mainScreen].bounds.size.height-400), 0);
        }];
    }

    
    NSLog(@"%@",note.userInfo) ;
}

-(void)btnSave
{
    if([self.delegate respondsToSelector:@selector(doSave)])
    {
        [self.delegate doSave];
    }
}
@end
